package com.atguigu.pojo;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Dom4jTest {
    @Test
    public void test1() throws DocumentException {
        // 创建一个SaxReader输入流，去读取 xml配置文件，生成Document对象
        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read("D:\\file\\code\\IdeaProjects\\JavaWeb\\05_xml\\src\\books.xml");
        Element rootElement = document.getRootElement();
        List<Element> book = rootElement.elements("book");
        for (Element element : book) {
//            System.out.println("element = " + element.asXML());
            Attribute sn = element.attribute("sn");
//            System.out.println("sn.getText() = " + sn.getText());
            Element name = element.element("name");
            Element price = element.element("price");
            Element author = element.element("author");
            Book book1 = new Book(sn.getText(),name.getTextTrim(),new BigDecimal(price.getText()),author.getText());
            System.out.println("book1 = " + book1);

        }

//        System.out.println(document);

    }
}
